package com.luinnx.message.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.luinnx.message.DB.DB;
import com.luinnx.message.model.Leavemessage;
import com.luinnx.message.model.User;

@SuppressWarnings("serial")
public class addMesServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置编码格式
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("UTF-8");
		
		//获取用户信息和留言信息
		HttpSession session=req.getSession();
		User user=(User)session.getAttribute("user");
		String title=req.getParameter("title");
		String type=req.getParameter("type");
		String content=req.getParameter("content");
		
		Leavemessage Leavemessage=new Leavemessage();
		Leavemessage.setTitle(title);
		Leavemessage.setType(type);
		Leavemessage.setContent(content);
		Leavemessage.setUserId(user.getId());
		Leavemessage.setDate(new Date(System.currentTimeMillis()));
		
		
		//执行插入操作	
		DB db=new DB();
		if(db.addMes(Leavemessage)){
			ArrayList<Leavemessage> al=db.findAll();
			session.setAttribute("al", al);
			
			resp.sendRedirect("main.jsp");
		}else{
			resp.sendRedirect("errorAddMes.jsp");
		}
		
		
		
	}
	
}
